﻿using Architecture.API.Application.Command.MenuCommand;
using Architecture.Domain;
using Architecture.Domain.Dto.MenuDto;
using Architecture.Domain.Service.MenuServices;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;

namespace Architecture.API.Controllers
{
    /// <summary>
    /// API控制器--菜单管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class MenuManagementController : ControllerBase
    {
        /// <summary>
        /// 命令总线
        /// </summary>
        protected readonly IMediator _mediator;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">命令总线</param>
        public MenuManagementController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 添加菜单信息
        /// </summary>
        /// <param name="command">菜单实体</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> CreateMenu(MenuCreateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await _mediator.Send(command);
                if (result.Code == -2)
                {
                    result.Message = "添加失败!该菜单名称已存在";
                }
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <param name="command">请求</param>
        /// <returns>返回菜单列表</returns>
        [HttpPost]
        public async Task<ApiResult<PagingMenuDto<MenuListDto>>> SearchMenuList(MenuSearchCommand command)
        {
            var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取sub的值(userId)
            ApiResult<PagingMenuDto<MenuListDto>> result = new ApiResult<PagingMenuDto<MenuListDto>>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 反填菜单信息
        /// </summary>
        /// <param name="command">请求菜单ID</param>
        /// <returns>返回菜单模型</returns>
        [HttpPost]
        public async Task<ApiResult<Menu>> FindMenuModel(MenuReverseFillingCommand command)
        {
            ApiResult<Menu> result = new ApiResult<Menu>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 修改菜单信息
        /// </summary>
        /// <param name="command">菜单实体</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateMenu(MenuUpdateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await _mediator.Send(command);
                if (result.Code == -2)
                {
                    result.Message = "修改失败!该菜单名称已存在";
                }
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 获取菜单数量
        /// </summary>
        /// <param name="command">请求菜单ID</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> SearchMenuCount(MenuSearchCountCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 删除菜单信息
        /// </summary>
        /// <param name="command">请求菜单ID</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeleteMenu(MenuDeletedCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 获取菜单部分信息列表
        /// </summary>
        /// <param name="command">请求</param>
        /// <returns>返回菜单列表</returns>
        [HttpPost]
        public async Task<ApiResult<List<MenuRecursionDto>>> SearchMenuInfo(MenuSearchInfoCommand command)
        {
            ApiResult<List<MenuRecursionDto>> result = new ApiResult<List<MenuRecursionDto>>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 获取动态菜单信息
        /// </summary>
        /// <param name="command">请求</param>
        /// <returns>返回动态菜单信息</returns>
        [HttpPost]
        public async Task<ApiResult<List<MenuDto>>> GetMenu(GetMenuCommand command)
        {
            ApiResult<List<MenuDto>> result = new ApiResult<List<MenuDto>>();
            try
            {
                result.Data= await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
    }
}